﻿/**
 * 
 * F. Mehmet ARSLAN (c) 2015
 * fmehmetarslan46@gmail.com
 *  
 **/
using ServiceState.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Threading;
using System.Web;

namespace ServiceState.Helper
{
    public class ServiceStateThread
    {
        private Thread stateThraed { get; set; }
        private int sleep;
        private int threadCount;
        private Queue<Service> servicesQue;

        public ServiceStateThread()
        {
            sleep = Int32.Parse(ConfigurationManager.AppSettings["refreshServiceState"] ?? "1000");
            ThreadStart tStart = new ThreadStart(refreshState);
            stateThraed = new Thread(tStart);
        }
        public void stop()
        {
            try
            {
                if (stateThraed.ThreadState != ThreadState.Suspended || stateThraed.ThreadState != ThreadState.Aborted)
                    stateThraed.Abort();
            }
            catch { }
        }
        public void start()
        {
            sleep = Int32.Parse(ConfigurationManager.AppSettings["refreshServiceState"] ?? "5000");
            threadCount = Int32.Parse(ConfigurationManager.AppSettings["threadCount"] ?? "5");
            ThreadStart tStart = new ThreadStart(this.refreshState);
            stateThraed = new Thread(tStart);
            stateThraed.Start();
        }

        private void refreshState()
        {
            while (true)
            {
                if (StateControl.ServiceStateList != null)
                {
                    ThreadStart tStart = new ThreadStart(this.getState);
                    List<Thread> childThread = new List<Thread>();                    
                    servicesQue = new Queue<Service>(StateControl.ServiceStateList);
                    for (int i = 0; i < threadCount; i++)
                    {
                        Thread thread = new Thread(tStart);
                        thread.Start();
                        childThread.Add(thread);

                    }
                }
                Thread.Sleep(sleep);
            }
        }

        private void getState()
        {
            while (servicesQue.Count > 0)
            {
                Service service = servicesQue.Dequeue();
                HttpStatusCode state = HttpStatusCode.SeeOther;
                if (service != null)
                {
                    switch (service.Method)
                    {
                        case Method.http: state = StateControl.httpControl(service.getUrl); break;
                        case Method.file: state = StateControl.fileControl(service.getUrl); break;
                    }
                    //var selected = StateControl.ServiceStateList.FirstOrDefault(x => x.Id.Equals(service.Id));
                    if (service.StatusCode.CompareTo(state) != 0)
                    {
                        LogManager.WriteStateChangeLog(service, new Service() { StatusCode = state });
                        service.StatusCode = state;
                    }
                }
            }
        }
    }
}